<?php
// +----------------------------------------------------------------------
// | Huncent Tinysupe [ SIMPLE IS THE BEST ! ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011 ZMRNet Studio, All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: tinysu <tinysu@qq.com>
// +----------------------------------------------------------------------

class kernel{
	var $db, $tm, $ft;
	
	function execute($file){
		is_file($file) or ht_error404();
		
		$db = $this->db;
		$tm = $this->tm;
		$ft = $this->ft;
		
		// assign
		$tm->assign('ft', $ft);
		$tm->assign('webext', _WEBEXT);
		$tm->assign('webdir', _WEBDIR);
		$tm->assign('webhost', $_SERVER['HTTP_HOST']);
		
		$tm->assign('sitedir', ht_sitedir());
		$tm->assign('skindir', ht_sitedir());
		
		// startup
		$start = ROOTDIR .'/extend/startup.php';
		if(is_file($start)) require($start);
		
		// require
		require($file);
	}
	
	function routing($pinfo){
		$purl = explode('/', $pinfo);
		if(empty($purl)) ht_error404();
		
		// node
		$node = array();
		foreach($purl as $val){
			if(is_numeric($val)) continue;
			if(trim($val) !='' ){
				$node[] = $val;
			}
		}
		if(empty($node)) ht_error404();

		// file
		$rdir = ROOTDIR .'/routes/';
		$file = $rdir .'err404.ctl.php';
		if(is_dir($rdir . $node[0])) $rdir .= $node[0] .'/';
		while(!empty($node)){
			$file = $rdir . implode('_', $node) .'.ctl.php';
			if(is_file($file)) break;
			array_pop($node);
		}
		
		// ctrler
		$this->execute($file);
	}
	
	function filter(){
		$regxs = array(); $qstrs = array();
		$rules = @$GLOBALS['config']['rules'];
		if(!is_array($rules)) ht_error404();
		foreach($rules as $regx => $qstr){
			$regxs[] = '/^\/'. $regx .'$/';
			$qstrs[] = $qstr;
		}
		
		$pinfo = $_SERVER['PATH_INFO'];
		$pinfo = preg_replace($regxs, $qstrs, $pinfo, 1);
		if(strstr($pinfo, '/')) ht_error404();
		parse_str($pinfo, $pstrs);
		return $pstrs;
	}

	
	function caching($time){
		if(!is_numeric($time)){
			$unit = substr($time, -1);
			$time = floatval(substr($time, 0, -1));
			switch($unit){
				case 'h': $time *= 3600; break;
				case 'd': $time *= 86400; break;
				case 'm': $time *= 2592000; break;
				case 'y': $time *= 31536000; break;
				default : $time *= 86400;
			}
		}
		$this->caching = $time;
	}
	
	function import($node){
		if($node == '') return false;
		if(is_file(ROOTDIR .'/models/'. $node .'.mod.php')){
			require_once(ROOTDIR .'/models/'. $node .'.mod.php');
			$model = 'mod_'. $node;
			if(class_exists($model)) return new $model();
		}
		return false;
	}
	
	private function tmdir() {
		$tmdir = ROOTDIR;
		$theme = @$GLOBALS['config']['theme'];
		if (!is_array($theme)) return $tmdir.'/theme/default';
		
		$pinfo = strtolower($_SERVER['PATH_INFO']);
		$route = explode('/', $pinfo);
		if (empty($route)) ht_error404();

		if (isset($theme[@$route[1]])) {
			$tmdir .= strtolower($theme[@$route[1]]);
		} else {
			$tmdir .= strtolower(@$theme['*']);
		}
		return $tmdir;
	}
	
	function __construct(){
		ob_start();
		defined('ROOTDIR') or die('Undefined ROOTDIR!');
		defined('HTMLEXT') or die('Undefined HTMLEXT!');

		// pathinfo
		$pinfo = @$_SERVER['PATH_INFO'];
		if(!REWRITE && substr($pinfo, -1)=='/'){
			$_SERVER['PATH_INFO'] = substr($pinfo, 0, -1);
			$pinfo = $_SERVER['PATH_INFO'];
		}
		if($pinfo=='') $_SERVER['PATH_INFO'] = '/index';
		$pinfo = strtolower($_SERVER['PATH_INFO']);
		
		/*
		$this->cache = $path . md5($this->querystr()) .'.cache';
		if(is_file($this->cache)){
			$data = file_get_contents($this->cache);
			if(intval(substr($data, 0, 10)) > time()){
				@header('content-type:text/html;charset=utf-8');
				exit(substr($data, 10));
			}
		}
		*/
		// init theme
		$this->tm = new theme();
		$this->tm->dir = $this->tmdir();

		// init dbase
		$this->db = new dbase();
		$GLOBALS['db'] = &$this->db;
		
		// filter
		$this->ft = $this->filter();
		
		// routing
		$this->routing($pinfo);
	}
	
	function __destruct(){
		// Storage the page caching
		/*
		if($this->errno == 0 && $this->caching > 0){
			$time = time() + $this->caching;
			file_put_contents($this->cache, $time . ob_get_flush());
		}
		*/
		unset($this->db, $this->tm);
	}
}
?>